package com.briup.codingFormat;

import java.util.HashMap;
import java.util.Map;

/**
 * @auther thb
 * @date 2021/9/17
 */
public class WayTest {

    public int[] twoSum(int[] nums, int target) {
        boolean flag = false;
        int arr[] = new int[2];
        for(int i=0;i<nums.length-1;i++){
            for(int j=i+1;j<nums.length;j++){
                int sum = nums[i]+nums[j];
                if(sum == target){
                    flag = true;
                    arr[0]=i;
                    arr[1]=j;
                    break;
                }
            }
            if(flag){
                break;
            }
        }
        return arr;
    }

    public int[] twoSum1(int[] nums, int target) {
        Map<Integer,Integer> map =new HashMap<>();
        for(int i=0;i< nums.length;i++){
            if(map.containsKey(target-nums[i])){
                return new int[]{map.get(target-nums[i]),i};
            }else{
                map.put(nums[i],i);
            }
        }
        return new int[0];
    }

}